import { asynsRouter, constantRoutes } from '@/router/index'

const state = {
  routes: [] // 声明变量，用于保存筛选出来的具有保存权限的动态路由
}

const mutations = {
  setRoutes(state, routes) {
    state.routes = [...constantRoutes, ...routes]
  }

}

const actions = {
  filterRoutes(context, menus) {
    const routes = asynsRouter.filter(item => {
      return menus.includes(item.code)
    })
    // 把筛选出来的路由保存在Vuex中
    context.commit('setRoutes', routes)
    return routes
  }
}

export default {
  namespaced: true, // 命名空间
  state,
  mutations,
  actions
}
